<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>我的团队管理界面</title>
    <link rel="icon" href="../assets/images/favicon.ico"/>
    <link rel="stylesheet" href="../assets/libs/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/module/formSelects/formSelects-v4.css" media="all">
    <link rel="stylesheet" href="../assets/common.css" media="all">
    <script src="../assets/libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="../assets/libs/jquery/jquery.cookie.js"></script>
</head>

<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div style="display: inline-block;width: 100px;margin-right: 10px;">
                <select id="sltKey">
                    <option value="">搜索条件</option>
                    <option value="team_name">团队名称</option>
                    <option value="guide_names">导游名称</option>
                </select>
            </div>
            <div style="display: inline-block;margin-right: 10px;width: 160px;">
                <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
            </div>
            <div class="layui-inline">
                <button id="teamBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
               <!-- <button id="teamBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
            </div>

            <table class="layui-table" id="teamTable" lay-filter="teamTable"></table>
        </div>
    </div>

</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn layui-btn-sm" lay-event="list">详细</a>
    <a class="layui-btn layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tbaleState">
    <input type="checkbox" lay-filter="ckStatu" lay-skin="switch" lay-text="正常|锁定"
           value="{{d.id}}" {{d.statu==0?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="teamForm">
    <form lay-filter="teamForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">团队名称</label>
            <div class="layui-input-block">
                <input name="teamName" placeholder="请输入团队名称" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">团队导游</label>
            <div class="layui-input-block">
                <select id="selGuide" name="guideId" xm-select="selGuide" xm-select-skin="default">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">团队游客</label>
            <div class="layui-input-block">
                <select name="personIds" xm-select="selPerson" xm-select-skin="default">

                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="teamFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/html" id="persons">
    <table class="layui-table" id="personTable" lay-filter="personTable"></table>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../assets/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        var formSelects = layui.formSelects;

        var mGuides=[];
        var mPersons=[];
        var mPersonsNotRepact=[];//去重后的游客

        var guidename=$.cookie('guidename');
        var id=$.cookie('id');
        // 渲染表格
        table.render({
            elem: '#teamTable',
            url: base_server + 'team/query',
            method: 'post',
            where: {
                guideId: id
            },
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'teamName', title: '团队名称'},
                {field: 'guideName', title: '导游名称'},
                {field: 'personNames', title: '旅游名称'},
                {
                    templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间',width:180
                },
                {field: 'statu', templet: '#tbaleState', title: '状态', unresize: true, width: 100},
                {align: 'center', toolbar: '#tableBar', title: '操作', unresize: true, width: 200}
            ]],
            skin: 'line',
            size: 'lg'
        });

        // 搜索按钮点击事件
        $('#teamBtnSearch').click(function () {
            var key = $('#sltKey').val();
            var value = $('#edtSearch').val();
            table.reload('teamTable', {where: {searchKey: key, searchValue: value}});
        });

        // 添加按钮点击事件
        $('#teamBtnAdd').click(function () {
            showAddModel();
        });

        // 工具条点击事件
        table.on('tool(teamTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm('确定删除团队吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('team/delete/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, 'DELETE');
                });
            } else if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }else if(layEvent === 'list'){ //查看游客详情
                showPersonList(data);
            }
        });

        //点击详情 显示弹窗表格
        function showPersonList(data) {
            console.log("点击详情传递过来的data:"+data);
            layer.open({
                type: 1,
                area: '1200px',
                offset: '30px',
                title: '团队游客',
                content: $('#persons').html(),
                success:function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    //渲染表格
                    table.render({
                        elem: '#personTable',
                        height:530,
                        url: base_server + 'person/queryByPersonIds',
                        method: 'post',
                        where: {
                            personIds:data.personIds
                        },
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers'},
                            {field: 'personName', title: '姓名'},
                            {field: 'phone', title: '手机号'},
                            {field: 'sex', title: '性别'},
                            {field: 'email', title: '邮箱'},
                            {field: 'address', title: '住址'},
                            {
                                templet: function (d) {
                                    return util.toDateString(d.createTime);
                                }, title: '创建时间',width:200
                            }
                        ]],
                        skin: 'line',
                        size: 'lg'
                    });

                }
            });
        }

        // 修改car状态
        form.on('switch(ckStatu)', function (obj) {
            layer.load(2);
            admin.req('team/statu', {
                id: obj.elem.value,
                statu: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'PUT');
        });

        // 显示表单弹窗
        function showEditModel(data) {
            console.log(data);
            layer.open({
                type: 1,
                area: '360px',
                offset: '65px',
                title: '修改团队信息',
                content: $('#teamForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.render();
                    //渲染 导游
                    var guideSelData=new Array();
                    for (var i=0;i<mGuides.length;i++){
                        guideSelData.push({name:mGuides[i].guideName,value:mGuides[i].id});
                    }
                    //formSelects.data('selGuide','local',{arr:guideSelData});
                    for(var i=0;i<guideSelData.length;i++){
                        $('#selGuide').append("<option value='"+guideSelData[i].value+"'>"+guideSelData[i].name+"</option>");
                    }
                    //渲染游客
                    var personSelData=new Array();
                    for (var i=0;i<mPersons.length;i++){
                        personSelData.push({name:mPersons[i].personName,value:mPersons[i].personId});
                    }
                    formSelects.data('selPerson','local',{arr:personSelData});
                    form.render('select');
                    // 回显team数据
                    if (data) {
                        form.val('teamForm', data);
                        var personIds=data.personIds;
                        var persons=personIds.split(',');
                        formSelects.value('selPerson',persons);
                    }
                    form.render('select');
                    // 表单提交事件
                    form.on('submit(teamFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('team/update', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page');
                                table.reload('teamTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        },'PUT');
                        return false;
                    });
                }
            });
        }

        function showAddModel() {
            layer.open({
                type: 1,
                area: '360px',
                offset: '65px',
                title: '添加团队信息',
                content: $('#teamForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.render();
                    //渲染 导游
                    var guideSelData=new Array();
                    for (var i=0;i<mGuides.length;i++){
                        guideSelData.push({name:mGuides[i].guideName,value:mGuides[i].id});
                    }
                    //formSelects.data('selGuide','local',{arr:guideSelData});
                    for(var i=0;i<guideSelData.length;i++){
                        $('#selGuide').append("<option value='"+guideSelData[i].value+"'>"+guideSelData[i].name+"</option>");
                    }
                    //渲染游客
                    var personSelData=new Array();
                    for (var i=0;i<mPersonsNotRepact.length;i++){
                        personSelData.push({name:mPersonsNotRepact[i].personName,value:mPersonsNotRepact[i].personId});
                    }
                    formSelects.data('selPerson','local',{arr:personSelData});
                    form.render('select');
                    // 表单提交事件
                    form.on('submit(teamFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('team/insert', d.field, function (res) {
                            console.log(d);
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                layer.closeAll('page');
                                table.reload('teamTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                        return false;
                    });
                }
            });
        }

        //获取导游
        admin.req('guide/queryAllStatu', {}, function (res) {
            layer.closeAll('loading');
            if (200 == res.code) {
                mGuides = res.data;
                for(var i=0;i<mGuides.length;i++) {
                    console.log("导游:"+mGuides[i].guideName);
                }
            } else {
                layer.msg('获取状态正常导游失败', {icon: 2});
            }
        }, 'GET');


        admin.req('person/queryAllStatu', {}, function (res) {
            layer.closeAll('loading');
            if (200 == res.code) {
                mPersons = res.data;
                for(var i=0;i<mPersons.length;i++) {
                    console.log("游客:"+mPersons[i].personName);
                }
            } else {
                layer.msg('获取状态正常游客失败', {icon: 2});
            }
        }, 'GET');

        //获取去重后的游客
        admin.req('person/queryNotRepact', {}, function (res) {
            layer.closeAll('loading');
            if (200 == res.code) {
                mPersonsNotRepact = res.data;
                for(var i=0;i<mPersonsNotRepact.length;i++) {
                    console.log("去重后游客:"+mPersonsNotRepact[i].personName);
                }
            } else {
                layer.msg('获取去重后游客失败', {icon: 2});
            }
        }, 'GET');

    });
</script>

</body>
</html>